package cn.echoedu.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import cn.echoedu.dao.DAODelete;
import cn.echoedu.dao.DAOFactory;
import cn.echoedu.daoconstants.QueryType;
import cn.echoedu.daoconstants.TableName;
import cn.echoedu.specifics.SpecMySql;
import cn.echoedu.util.DbUtil;
import cn.echoedu.util.L;

public class MySqlDelete implements DAODelete {
	DAOFactory fac=null;
	private final String T="MySqlDelete------->";
	public MySqlDelete(MySqlFactory mySqlFactory) {
		// TODO Auto-generated constructor stub
		fac=mySqlFactory;
	}
	public <T> boolean deleteFrom(Connection con, TableName tableName,
			T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		PreparedStatement ps=null;
		final String M = "deleteFrom********>>";
		L.p(T+M);
		try{
			if(!fac.getDAORead().<T> alreadyExisting(con,tableName,currentPojo)){
				return false;
			}
			String primaryKey=SpecMySql.<T>getPrimaryKey(tableName,currentPojo);
			ps=con.prepareStatement(SpecMySql.queryString(tableName,
					primaryKey,QueryType.DELETE));
			L.p(T+M+ps.toString());
			ps.execute();
			con.commit();
		}catch(Throwable e){  
			if(con!=null){  
				try {  
					DbUtil.rollback(con);
				} catch (SQLException e1) {  
					e1.printStackTrace();  
				}  
			}  
		}
		finally{
			DAOFactory.closeAll(con,ps, null);
		}
		return true;
	}
	public boolean deleteByColumnFrom(Connection connection,
			TableName tablename, String column, String value) {
		// TODO Auto-generated method stub
		final String M = "deleteFrom********>>";
		L.p(T+M);
		String sql = "delete from "+tablename+" where "+column+"='"+value+"'";
		return deleteBySql(connection,tablename,sql);
	}
	public boolean deleteBySql(Connection connection,
			TableName tablename, String sql) {
		// TODO Auto-generated method stub
		Statement statement = null;
		final String M = "deleteFrom********>>";
		L.p(T+M);
		boolean result = false;
		try{
			statement = connection.createStatement();
			result = statement.execute(sql);
			connection.commit();
			return result;
		} catch(Throwable e){  
			if(connection!=null){  
				try {  
					DbUtil.rollback(connection);  
				} catch (SQLException e1) {  
					e1.printStackTrace();  
				}  
			}  
		}finally{
			DAOFactory.closeAll(connection,statement, null);
		}
		return result;
	}
	public boolean deleteListBySqls(Connection conn,TableName tablename,List<String> sqls){
		Statement statement = null;
		try {
			  statement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
            for(String sql:sqls){
            	statement.addBatch(sql);
            }
            statement.executeBatch();
            return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			if(conn!=null){  
				try {  
					DbUtil.rollback(conn);  
				} catch (SQLException e1) {  
					e1.printStackTrace();  
				}  
			}  
		}finally{
			DAOFactory.closeAll(conn,statement, null);
		}
		return false;


	}

}
